package com.xc.erp.modules.erp.controller.role;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xc.erp.common.annotation.Log;
import com.xc.erp.modules.erp.domain.AjaxResult;
import com.xc.erp.modules.erp.domain.pojo.JshRole;
import com.xc.erp.modules.erp.domain.pojo.JshUserRole;
import com.xc.erp.modules.erp.service.JshRoleFunctionService;
import com.xc.erp.modules.erp.service.JshRoleService;
import com.xc.erp.modules.erp.service.JshUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;

/**
 * 角色管理
 */
@Api(tags = "角色管理",value = "role接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private JshRoleService jshRoleService;

    @Autowired
    private JshRoleFunctionService jshRoleFunctionService;

    /**
     * 分页查看列表
     *
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @param name 角色名称
     * @return 角色列表集合
     */
    @ApiOperation("角色列表")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public AjaxResult list(@PathVariable Integer pageNum, @PathVariable Integer pageSize, String name) {
        Page<JshRole> roleList = jshRoleService.getRoleList(pageNum, pageSize, name);
        return AjaxResult.success(roleList);
    }

    /**
     * 新增
     */
    @Log(modul = "角色管理",desc = "新增")
    @ApiOperation("角色新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody JshRole jshRole) {
        jshRole.setEnabled(true);
        jshRoleService.getBaseMapper().insert(jshRole);
        return AjaxResult.success();
    }

    /**
     * 修改角色
     *
     * @param jshRole 角色信息
     * @return 结果
     */
    @Log(modul = "角色管理",desc = "修改")
    @ApiOperation("角色修改")
    @PutMapping("update")
    public AjaxResult update(@RequestBody JshRole jshRole) {
        jshRoleService.getBaseMapper().updateById(jshRole);
        return AjaxResult.success();

    }

    /**
     * 删除角色
     * @param id 角色编号
     * @return 结果
     */
    @Log(modul = "角色管理",desc = "删除")
    @ApiOperation("删除角色")
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        Integer[] ids = {id};
        if(jshRoleService.isUserNotNull(ids)){
            return AjaxResult.error("该角色下存在用户");
        } else {
            if(jshRoleService.deleteByRoleId(id)){
                return AjaxResult.success();
            }else {
                return AjaxResult.error("角色删除失败");
            }
        }
    }

    /**
     * 批量删除
     * @param ids 角色编号集合
     * @return 结果
     */
    @Log(modul = "角色管理",desc = "批量删除")
    @ApiOperation("批量删除角色")
    @DeleteMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody Integer[] ids) {
        if (jshRoleService.isUserNotNull(ids)) {
            return AjaxResult.error("该角色下存在用户");
        }
        if(jshRoleService.deleteByRoleIdBatch(ids)){
            return AjaxResult.success();
        } else {
            return AjaxResult.error("批量删除异常");
        }
    }

    /**
     * 判断角色名称是否存在
     */
    @ApiOperation("判断角色是否存在")
    @PostMapping("/checkIsNameExist")
    public AjaxResult checkIsNameExist(Long id,String name) {
        boolean status = jshRoleService.checkIsNameExist(id,name);
        return AjaxResult.success().put("status",status);
    }


    /**
     * 批量启用/禁用
     */
    @Log(modul = "角色管理",desc = "启用/禁用")
    @ApiOperation("批量启用/禁用")
    @PostMapping("/batchSetStatus/{status}")
    public AjaxResult batchSetStatus(@RequestBody Long[] ids,@PathVariable Boolean status) {
        jshRoleService.batchSetStatus(ids, status);
        return AjaxResult.success();
    }

    /**
     * 添加权限功能
     * @param functionIds 权限功能列表
     * @param roleId 角色编号
     * @return 结果
     */
    @Log(modul = "角色管理",desc = "分配功能")
    @ApiOperation("分配功能")
    @PostMapping("addRoleAndFunction/{roleId}")
    public AjaxResult insertRoleAndFunction(@RequestBody Long[] functionIds,@PathVariable Long roleId){
        if(jshRoleFunctionService.addRoleAndFunction(functionIds,roleId)){
            return AjaxResult.success();
        }
        return AjaxResult.error("分配功能异常");
    }

}
